package com.xiaomiMall.mange.service.Impl;

import com.ruoyi.common.utils.StringUtils;
import com.xiaomiMall.mange.domain.MallUser;
import com.xiaomiMall.mange.domain.vo.Address;
import com.xiaomiMall.mange.domain.vo.Notepad;
import com.xiaomiMall.mange.domain.vo.Order;
import com.xiaomiMall.mange.domain.vo.OrderShow;
import com.xiaomiMall.mange.mapper.MallUserMapper;
import com.xiaomiMall.mange.service.MallUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class MallUserServiceImpl implements MallUserService {

    @Resource
    private MallUserMapper mallUserMapper;

    @Override
    public MallUser selectMallUserByEmail(String email) {
        return mallUserMapper.selectMallUserByEmail(email);
    }

    @Override
    public MallUser selectMallUserById(Long id) {
        return mallUserMapper.selectMallUserById(id);
    }

    @Override
    public Boolean checkEmailUnique(String email) {
        MallUser mallUser = mallUserMapper.selectMallUserByEmail(email);
        return StringUtils.isNotNull(mallUser);
    }

    @Override
    public Boolean registerUser(MallUser mallUser) {
        Integer count = mallUserMapper.insertMallUser(mallUser);
        return count == 1;
    }

    /**
     * 修改密码
     * @param user 商城用户
     * @return 结果
     */
    @Override
    public Boolean updatePassword(MallUser user) {
        int i = mallUserMapper.updatePassword(user);
        return i > 0;
    }

    /**
     * 查询地址信息
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public List<Address> getAddress(Long userId) {
        return mallUserMapper.getAddress(userId);
    }

    /**
     * 新增收货地址
     * @param address 地址对象
     * @return 结果
     */
    @Override
    public Boolean addAddress(Address address) {
        int i = mallUserMapper.addAddress(address);
        return i > 0;
    }

    /**
     * 修改收货地址
     * @param address 收货地址对象
     * @return 结果
     */
    @Override
    public Boolean modifyAddress(Address address) {
        int i = mallUserMapper.modifyAddress(address);
        return i > 0;
    }

    /**
     * 删除收货地址
     * @param userId 登陆用户的id
     * @param id     地址的id
     * @return 结果
     */
    @Override
    public Boolean deleteAddress(Long userId, int id) {
        int i = mallUserMapper.deleteAddress(userId, id);
        return i > 0;
    }

    /**
     * 由订单号获取订单对象
     * @param userId   用户id
     * @param orderNum 订单号码
     * @return 同一订单号的所有订单
     */
    @Override
    public List<Order> selectOrderByOrderNum(Long userId, Long orderNum) {
        return mallUserMapper.selectOrderByOrderNum(userId,orderNum);
    }

    /**
     * 由订单状态获取订单号
     * @param userId 登陆用户id
     * @param status 订单的状态
     * @return 订单号的list
     */
    @Override
    public List<String> getOrderBystatus(Long userId,Integer status) {
        return mallUserMapper.getOrderBystatus(userId, status);
    }

    /**
     * 根据订单号查询订单详情
     * @param orderNumber 订单号
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public OrderShow getOrderShowByNum(Long userId, String orderNumber) {
        List<OrderShow> orderShowList = mallUserMapper.getOrderShowByNum(userId, orderNumber);
        OrderShow orderShow = new OrderShow();
        orderShow.setDealTime(orderShowList.get(0).getDealTime());
        orderShow.setOrderStatus(orderShowList.get(0).getOrderStatus());
        orderShow.setOrderNumber(orderShowList.get(0).getOrderNumber());
        orderShow.setCity(orderShowList.get(0).getCity());
        orderShow.setCost(orderShowList.get(0).getCost());
        List<String> products = new ArrayList<>();
        List<String> amounts = new ArrayList<>();
        for (OrderShow os : orderShowList) {
            products.add(os.getProductName());
            amounts.add("x" + os.getAmount());
        }
        orderShow.setProductNames(products);
        orderShow.setAmounts(amounts);
        return orderShow;
    }

    /**
     * 获取传往商城页面上的OrderShow对象
     * @param userId 登陆用户的id
     * @param status 订单状态
     * @return 结果
     */
    public List<OrderShow> getOrderShow(Long userId, int status) {
        List<String> orderNumbers = getOrderBystatus(userId, status);
        List<OrderShow> orderShows = new ArrayList<>();
        for (String orderNum : orderNumbers) {
            OrderShow orderShow = getOrderShowByNum(userId, orderNum);
            orderShows.add(orderShow);
        }
        return orderShows;
    }

    /**
     * 由商品名查询到订单历史
     * @param userId   用户id
     * @param prodName 商品名
     * @return 结果
     */
    @Override
    public List<OrderShow> getOrderShowByName(Long userId, String prodName) {
        List<OrderShow> orderByName = mallUserMapper.getOrderByName(userId, prodName);
        HashSet<String> orderNumbers = new HashSet<>();
        for (OrderShow orderShow : orderByName) {
            orderNumbers.add(orderShow.getOrderNumber());
        }
        List<OrderShow> orderShowList = new ArrayList<>();
        for (String orderNum : orderNumbers) {
            OrderShow orderShow = getOrderShowByNum(userId, orderNum);
            orderShowList.add(orderShow);
        }
        return orderShowList;
    }

    /**
     * 更新订单状态
     * @param orderNumber 订单号
     * @param status   新的订单状态
     * @return 结果
     */
    @Override
    public Boolean updateOrderStatus(String orderNumber, int status) {
        int i = mallUserMapper.updateOrderStatus(orderNumber, status);
        return i > 0;
    }

    /**
     * 插入订单记录
     * @param orderList order对象组成的list
     * @return 结果
     */
    @Override
    public Boolean insertOrder(List<Order> orderList) {
        int a = mallUserMapper.insertOrder(orderList);
        return a>=orderList.size();
    }

    /**
     * 创建订单
     * @param ids 购物车表的id的集合
     * @return 订单集合
     */
    @Override
    public List<Order> creatOrder(List<Integer> ids, Long addressId) {
        List<Order> orders = mallUserMapper.creatOrder(ids);
        for (Order order : orders) {
            Date d = new Date();
            order.setTimeBuying(d);
            order.setOrderNumber(d.getTime());
            order.setOrderStatus(2);
            order.setAddressId(addressId);
        }
        return orders;
    }

    /**
     * 新增申请消息（充值申请）
     * @param notepad 消息对象
     * @return 结果
     */
    @Override
    public Boolean insertNotepad(Notepad notepad) {
        int i = mallUserMapper.insertNotepad(notepad);
        return i > 0;
    }

    /**
     * @program: ${PROJECT_NAME}
     * @description : 修改用户昵称  ShoppingCart
     * @Date : 21:44 2021/11/15
     * @author : codefish
     * @return:
     */
    @Override
    public boolean updateNickName(String userName, String userEmail) {
        boolean flag = true;
        int count = mallUserMapper.updateNickName(userName, userEmail);
        if (count != 1){
            flag = false;
        }
        return flag;
    }

    /**
     * 获取用户的消息通知
     * @param userId 用户id
     * @param status 消息状态
     * @return 结果
     */
    @Override
    public List<Notepad> selectNotepadByUserId(Long userId, Integer status) {
        return mallUserMapper.selectNotepadByUserId(userId, status);
    }

    /**
     * 扣费
     * @param userId 用户id
     * @param money  被扣的钱数
     * @return 结果
     */
    @Override
    public Boolean reduceWallet(Long userId, float money) {
        return mallUserMapper.reduceWallet(userId,money) > 0;
    }
}
